package atpa.partial;

public class ATPAPartial {

    public void Josephus(int noduri, int deleteEvery) {
        Nod t = new Nod(1);
        Nod x = t;
        for (int i = 2; i <= noduri; i++) {
            x.urm = new Nod(i);
            x = x.urm;
        }
        x.urm = t;
        x = t;
        while (x != x.urm) {
            for (int i = 1; i < deleteEvery - 1; i++) {
                x = x.urm;
            }
            x.urm = x.urm.urm;
            x = x.urm;
        }
        System.out.println("Valoarea = " + x.val);
    }

    public void Eratostene(int N) {
        boolean[] a = new boolean[N];
        for (int i = 2; i < N; i++) {
            a[i] = true;
        }
        for (int i = 2; i < N; i++) {
            if (a[i] != false) {
                for (int j = i; j * i < N; j++) {
                    a[j * i] = false;
                }
            }
        }
        for (int i = 2; i < N; i++) {
            if (a[i] == true) {
                System.out.println(i);
            }
        }
    }

    public void expresie(String expr) {
        char[] vect = expr.toCharArray();
        System.out.println(vect);
        StivaL s = new StivaL(vect.length);
        for (int i = 0; i < vect.length; i++) {
            System.out.print("char = " + vect[i]);
            if (vect[i] == '{' || vect[i] == '[' || vect[i] == '(') {
                s.push(vect[i]);
                System.out.print(" l-am bagat in stiva ");
            } else if (vect[i] == '}' || vect[i] == ']' || vect[i] == ')') {
                System.out.print(" compar, e egal cu },],) ");
                if (vect[i] == '}' && s.peek() == '{') {
                    s.pop();
                    System.out.print(" l-am scos din stiva ");
                    continue;
                } else if (vect[i] == ']' && s.peek() == '[') {
                    s.pop();
                    System.out.print(" l-am scos din stiva ");
                    continue;
                } else if (vect[i] == ')' && s.peek() == '(') {
                    s.pop();
                    System.out.print(" l-am scos din stiva ");
                    continue;
                } else {
                    System.out.println("\n***Eroare element: " + vect[i] + ", pozitia=" + i + "***");
                }

            }
            System.out.println("");
        }
        if (!s.isEmpty()) {
            System.out.println("Eroare la primul element !!!");
        }
    }

    public void selectionSort(int[] x) {
        for (int i = 0; i < x.length - 1; i++) {
            for (int j = i + 1; j < x.length; j++) {
                if (x[i] > x[j]) {
                    int temp = x[i];
                    x[i] = x[j];
                    x[j] = temp;
                }
            }
        }
        for (int i = 0; i < x.length; i++) {
            System.out.println(x[i] + " ");
        }
    }

    public void insertionSort(int[] tab) {
        int tmp, j;
        for (int i = 0; i < tab.length; i++) {
            tmp = tab[i];
            j = i;
            while (j > 0 && tmp < tab[j - 1]) {
                tab[j] = tab[j - 1];
                j--;
            }
            tab[j] = tmp;
        }
        for (int i = 0; i < tab.length; i++) {
            System.out.println(tab[i]);
        }
    }
    
    public void bubbleSort(int[] vect) {
        boolean gasit = true;
        int aux = 0;
//        long start = System.currentTimeMillis();
        while (gasit) {
            gasit = false;
            for (int i = 0; i < vect.length - 1; i++) {
                if (vect[i] > vect[i + 1]) {
                    aux = vect[i];
                    vect[i] = vect[i + 1];
                    vect[i + 1] = aux;
                    gasit = true;
                }
            }
        }
//        long end = System.currentTimeMillis();
//        return Arrays.toString(vect);
        for (int i = 0; i < vect.length - 1; i++) {
            System.out.println(vect[i]);
        }
    }

    public static void main(String[] args) {
        ATPAPartial a = new ATPAPartial();
        int[] vals = {431, 545, 3, 5, 6, 78, 65, 4, 234, 5, 3, 42, 3, 452};
        a.bubbleSort(vals);
    }
}
